package protocol.transactions;

import protocol.XoneProtocol;
import protocol.XoneServiceCode;

import com.google.gson.JsonElement;
import com.netease.http.THttpRequest;


public class TransactionUpload extends XoneBaseTransaction {
    private UploadPrama mUploadPrama;

    public TransactionUpload(UploadPrama prama) {
        super(TRANSACTION_TYPE_UPLOAD);
        this.mUploadPrama = prama;
    }

    @Override
    protected void onXoneTransactionSuccess(int code, Object obj) {
        if (obj instanceof JsonElement) {
            String value = null;
            try {
                JsonElement json = (JsonElement)obj;
                value = json.getAsJsonObject().get("url").getAsString();
            } catch (Exception e) {
                e.printStackTrace();
            }
            notifyMessage(XoneServiceCode.TRANSACTION_SUCCESS, value);
        }
    }
    
    @Override
    public void onTransact() {
        final UploadPrama prama = mUploadPrama;
        THttpRequest request = XoneProtocol.getInstance().createUploadRequest(prama);
        sendRequest(request);
        mUploadPrama = null;
    }
    
    public static class UploadPrama {
        public static final int TYPE_BASE = 1;
        public static final int TYPE_IMAGE = TYPE_BASE;
        public static final int TYPE_VIDEO = TYPE_BASE + 1;
        public static final int TYPE_AUDIO = TYPE_BASE + 2;
        
        // 文件路径
        public String path;
        // 1，图片；2，视频；3，音频；
        public int    type;
        // 当type为音频时，音频文件播放时长
        public int duration;
        
        // 转发评论时资讯id
        public String infoId;
        // 是否同时被转发或评论标志
        public boolean flag;
    }
}
